package cn.hznc.controller.life;

import cn.hznc.aspect.BusinessType;
import cn.hznc.aspect.Log;
import cn.hznc.controller.service.toollife.ToolLieService;
import cn.hznc.domain.entity.ToolLifeEntity;
import cn.hznc.domain.request.common.QueryCondition;
import cn.hznc.domain.request.life.QryToolLifeConfigReq;
import cn.hznc.domain.request.life.QueryToolLifeReq;
import cn.hznc.domain.request.life.ToolUsageReportReq;
import cn.hznc.domain.request.toollife.EditOrAddToolLifeConfigReq;
import cn.hznc.domain.request.toollife.ToolLifeDataIndexReq;
import cn.hznc.domain.response.ResponseBody;
import cn.hznc.exception.ToolLifeCycleException;
import cn.hznc.exception.ToolScheduleException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author：Administrator
 * @Date：2023/6/8 0:25
 */
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/toolLife")
@Slf4j
public class ToolLifeController{

    @Autowired
    private ToolLieService toolLieService;

    /**
     * 刀具寿命管理首页
     *
     * @return
     */
    @RequestMapping(value = "/queryToolLifeIndex", method = RequestMethod.POST)
    public ResponseBody queryToolLifeIndex(@RequestBody QueryToolLifeReq queryToolLifeReq) {
        return ResponseBody.success(toolLieService.queryToolLifeIndex(queryToolLifeReq));
    }

//    @RequestMapping(value = "/editOrAddToolLifeConfig", method = RequestMethod.POST)
//    public ResponseBody editOrAddToolLifeConfig(@RequestBody EditOrAddToolLifeConfigReq editOrAddToolLifeConfigReq) {
//        return ResponseBody.success(toolLieService.editOrAddToolLifeConfig(editOrAddToolLifeConfigReq));
//    }

    @RequestMapping(value = "/qryToolLifeConfig", method = RequestMethod.POST)
    public ResponseBody qryToolLifeConfig(@RequestBody QryToolLifeConfigReq qryToolLifeConfigReq) {
        return ResponseBody.success(toolLieService.qryToolLifeConfig(qryToolLifeConfigReq));
    }

    @RequestMapping(value = "/qryToolLifeType", method = RequestMethod.GET)
    public ResponseBody qryToolLifeType(@RequestParam("type")String type) {
        return ResponseBody.success(toolLieService.qryToolLifeType(type));
    }


    @RequestMapping(value = "/addOrEditToolLifeConfig", method = RequestMethod.POST)
    @Log(title = "新增或编辑刀具寿命配置" , businessType = BusinessType.OTHER)
    public ResponseBody addToolLifeConfig(@RequestBody ToolLifeEntity ToolLifeEntity) {
        try {
            return ResponseBody.success(toolLieService.addToolLifeConfig(ToolLifeEntity));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode() , e.getMsg());
        }
    }

    @RequestMapping(value = "/deleteToolLifeConfig", method = RequestMethod.GET)
    @Log(title = "删除刀具寿命配置" , businessType = BusinessType.DELETE)
    public ResponseBody deleteToolLifeConfig(@RequestParam("id") Integer id) {
        return ResponseBody.success(toolLieService.deleteToolLifeConfig(id));
    }

    @RequestMapping(value = "/qryToolLifeUsageHistory", method = RequestMethod.GET)
    public ResponseBody qryToolLifeUsageHistory(@RequestParam("uniqueId") String uniqueId,
                                                @RequestParam("pageNum") Integer pageNum,
                                                @RequestParam("pageSize") Integer pageSize) {
        return ResponseBody.success(toolLieService.qryToolLifeUsageHistory(uniqueId , pageNum , pageSize));
    }

    @RequestMapping(value = "/qryToolLifeUsageHistoryList", method = RequestMethod.GET)
    public ResponseBody qryToolLifeUsageHistoryList(@RequestParam("uniqueId") String uniqueId,
                                                @RequestParam("pageNum") Integer pageNum,
                                                @RequestParam("pageSize") Integer pageSize) {
        return ResponseBody.success(toolLieService.qryToolLifeUsageHistoryList(uniqueId , pageNum , pageSize));
    }

    @RequestMapping(value = "/qryToolPartUsageHistoryList", method = RequestMethod.GET)
    public ResponseBody qryToolPartUsageHistoryList(@RequestParam("uniqueId") String uniqueId,
                                                    @RequestParam("pageNum") Integer pageNum,
                                                    @RequestParam("pageSize") Integer pageSize) {
        return ResponseBody.success(toolLieService.qryToolPartUsageHistoryList(uniqueId , pageNum , pageSize));
    }

    @RequestMapping(value = "/toolUsageReport", method = RequestMethod.POST)
    public ResponseBody toolUsageReport(@RequestBody ToolUsageReportReq toolUsageReportReq) {
        try {
            return ResponseBody.success(toolLieService.toolUsageReport(toolUsageReportReq));
        } catch (ToolScheduleException e) {
            return ResponseBody.error(e.getCode() , e.getMsg());
        }
    }

    @RequestMapping(value = "/toolUsageProgramReport", method = RequestMethod.POST)
    public ResponseBody toolUsageProgramReport(@RequestBody ToolUsageReportReq toolUsageReportReq) {
        try {
            return ResponseBody.success(toolLieService.toolUsageReport(toolUsageReportReq));
        } catch (ToolScheduleException e) {
            return ResponseBody.error(e.getCode() , e.getMsg());
        }
    }


    @RequestMapping(value = "/toolLifeDataIndex", method = RequestMethod.POST)
    public ResponseBody toolLifeDataIndex(@RequestBody ToolLifeDataIndexReq toolLifeDataIndexReq) {
            return ResponseBody.success(toolLieService.toolLifeDataIndex(toolLifeDataIndexReq));
    }

    @RequestMapping(value = "/toolLifeData", method = RequestMethod.GET)
    public ResponseBody toolLifeData(@RequestParam("toolNo") String toolNo,
                                     @RequestParam("pageNum") Integer pageNum,
                                     @RequestParam("pageSize") Integer pageSize) {
        return ResponseBody.success(toolLieService.toolLifeData(toolNo , pageNum , pageSize));
    }

    @RequestMapping(value = "/getByUniqueId", method = RequestMethod.GET)
    public ResponseBody getByUniqueId(@RequestParam("uniqueId") String uniqueId) {
        return ResponseBody.success(toolLieService.getByUniqueId(uniqueId));
    }

    @RequestMapping(value = "/getGroupIdnr", method = RequestMethod.GET)
    public ResponseBody getGroupIdnr() {
        return ResponseBody.success(toolLieService.getGroupIdnr());
    }

}

